package com.ruoyi.zxjd.exposure.service.impl;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.SmsUtil;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.zxjd.common.CommonConstants;
import com.ruoyi.zxjd.common.domain.CommonUserVO;
import com.ruoyi.zxjd.common.mapper.CommonMapper;
import com.ruoyi.zxjd.conference.domain.ConferenceArrange;
import com.ruoyi.zxjd.conference.domain.ConferenceProgress;
import com.ruoyi.zxjd.exposure.service.IExposureProgressService;
import com.ruoyi.zxjd.work.domain.WorkHandle;
import com.ruoyi.zxjd.work.service.IWorkHandleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.TimerTask;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.zxjd.exposure.domain.ExposureProgress;
import com.ruoyi.zxjd.exposure.mapper.ExposureArrangeMapper;
import com.ruoyi.zxjd.exposure.domain.ExposureArrange;
import com.ruoyi.zxjd.exposure.service.IExposureArrangeService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 曝光台列表Service业务层处理
 * 
 * @author lcc
 * @date 2021-02-07
 */
@Service
public class ExposureArrangeServiceImpl implements IExposureArrangeService 
{
    @Resource
    private ExposureArrangeMapper exposureArrangeMapper;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IExposureProgressService exposureProgressService;

    @Autowired
    private IWorkHandleService workHandleService;

    @Resource
    private CommonMapper commonMapper;

    /**
     * 查询曝光台列表
     * 
     * @param arrangeId 曝光台列表ID
     * @return 曝光台列表
     */
    @Override
    public ExposureArrange selectExposureArrangeById(Long arrangeId)
    {
        return exposureArrangeMapper.selectExposureArrangeById(arrangeId);
    }

    /**
     * 查询曝光台列表列表
     * 
     * @param exposureArrange 曝光台列表
     * @return 曝光台列表
     */
    @Override
    public List<ExposureArrange> selectExposureArrangeList(ExposureArrange exposureArrange)
    {
        return exposureArrangeMapper.selectExposureArrangeList(exposureArrange);
    }

    /**
     * 新增曝光台列表
     * 
     * @param exposureArrange 曝光台列表
     * @return 结果
     */
    @Transactional
    @Override
    public int insertExposureArrange(ExposureArrange exposureArrange)
    {
        exposureArrange.setCreateBy(sysUserService.selectUserByLoginName(ShiroUtils.getLoginName()).getUserName());
        exposureArrange.setCreateTime(DateUtils.getNowDate());
        int rows = exposureArrangeMapper.insertExposureArrange(exposureArrange);
        //insertExposureProgress(exposureArrange);
        return rows;
    }

    /**
     * 修改曝光台列表
     * 
     * @param exposureArrange 曝光台列表
     * @return 结果
     */
    @Transactional
    @Override
    public int updateExposureArrange(ExposureArrange exposureArrange)
    {
        exposureArrange.setUpdateBy(sysUserService.selectUserByLoginName(ShiroUtils.getLoginName()).getUserName());
        exposureArrange.setUpdateTime(DateUtils.getNowDate());
        exposureArrangeMapper.deleteExposureProgressByArrangeId(exposureArrange.getArrangeId());
        //insertExposureProgress(exposureArrange);
        return exposureArrangeMapper.updateExposureArrange(exposureArrange);
    }

    /**
     * 删除曝光台列表对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteExposureArrangeByIds(String ids)
    {
        exposureArrangeMapper.deleteExposureProgressByArrangeIds(Convert.toStrArray(ids));
        return exposureArrangeMapper.deleteExposureArrangeByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除曝光台列表信息
     * 
     * @param arrangeId 曝光台列表ID
     * @return 结果
     */
    @Override
    public int deleteExposureArrangeById(Long arrangeId)
    {
        exposureArrangeMapper.deleteExposureProgressByArrangeId(arrangeId);
        return exposureArrangeMapper.deleteExposureArrangeById(arrangeId);
    }

    /**
     * 曝光安排
     * @param arrangeId
     * @param draft
     * @return
     */
    @Override
    @Transactional
    public int updateStatusByArrangeId(Long arrangeId, String draft) {
        ExposureArrange exposureArrange = exposureArrangeMapper.selectExposureArrangeById(arrangeId);
        exposureArrange.setStatus(draft);
        SysUser sysUser = sysUserService.selectUserByLoginName(ShiroUtils.getLoginName());
        exposureArrange.setUserId(sysUser.getUserId());
        exposureArrange.setDeptId(sysUser.getDeptId());
        exposureArrange.setUserName(sysUser.getUserName());
        exposureArrange.setDeptName(sysUser.getDept().getDeptName());
        if ("1".equals(draft)){
            //exposureArrangeMapper.updateExposureArrange(exposureArrange);
            insertExposureProgress(exposureArrange);
        }
        if ("0".equals(draft)){
            exposureArrangeMapper.deleteExposureProgressByArrangeId(arrangeId);
        }
        return exposureArrangeMapper.updateStatusByArrangeId(arrangeId,draft);
    }

    /**
     * 新增查看进度信息
     * 
     * @param exposureArrange 曝光台列表对象
     */
    public void insertExposureProgress(ExposureArrange exposureArrange)
    {

        String[] ids = new String[0];
        String releaseScope = exposureArrange.getReleaseScope();
        String[] userNames = releaseScope.split(" ");
        for (int i=0;i<userNames.length;i++){
            SysUser sysUser = sysUserService.selectUserByName(userNames[i]);
            ids = Arrays.copyOf(ids, ids.length + 1); // 数组增加一个长度
            ids[ids.length - 1] = sysUser.getUserId()+""; // 在增加的位置赋值需要添加的数据
        }
        exposureArrange.setUserIds(ids);
        String[] userIds = exposureArrange.getUserIds();
        List<CommonUserVO> list = null;

        if(CommonConstants.AllUSER.equals(releaseScope)){
            list = commonMapper.listActiveUser(null,null);
        }else{
            list = commonMapper.listActiveUserByIds(userIds);
        }
        List<ExposureProgress> exposureProgressList = new ArrayList<>();
        if(StringUtils.isNotNull(list)) {
            for (int i = 0; i < list.size(); i++) {
                CommonUserVO userVO = list.get(i);
                ExposureProgress exposureProgress = new ExposureProgress();
                exposureProgress.setArrangeId(exposureArrange.getArrangeId());
                exposureProgress.setArrangeName(exposureArrange.getArrangeTitle());
                exposureProgress.setReceiveUserId(userVO.getUserId());
                exposureProgress.setReceiveDeptId(userVO.getDeptId());
                exposureProgress.setReceiveUser(userVO.getUserName());
                exposureProgress.setReceiveDept(userVO.getDeptName());
                exposureProgress.setStatus("0");
                exposureProgress.setCreateBy(exposureArrange.getCreateBy());
                exposureProgress.setCreateTime(exposureArrange.getCreateTime());
                exposureProgress.setRemark(exposureArrange.getRemark());
                SysUser sysUser = sysUserService.selectUserByLoginName(ShiroUtils.getLoginName());
                exposureProgress.setUserId(sysUser.getUserId());
                exposureProgress.setUserName(sysUser.getUserName());
                exposureProgress.setDeptId(sysUser.getDeptId());
                exposureProgress.setDeptName(sysUser.getDept().getDeptName());
                exposureProgressList.add(exposureProgress);
                //每50条进行新增插入
                if (exposureProgressList.size() % 50 == 0 || i == list.size() - 1) {
                    exposureArrangeMapper.batchExposureProgress(exposureProgressList);
                    exposureProgressList.clear();
                }
            }

            if (StringUtils.equals(CommonConstants.PUBLISHED, exposureArrange.getStatus())) {
                //短信提示
                if (StringUtils.equals(CommonConstants.TIPS, exposureArrange.getSmsTips())) {
                    AsyncManager.me().execute(createSmsTips(exposureArrange.getArrangeTitle(), list,exposureArrange));
                }
                //平台提示
                if (StringUtils.equals(CommonConstants.TIPS, exposureArrange.getWebsiteTips())) {
                    AsyncManager.me().execute(createWebsiteTips(list, 1));
                }
            }
        }
    }

    private TimerTask createSmsTips(String arrangeTitle, List<CommonUserVO> list, ExposureArrange exposureArrange) {
        return new TimerTask()
        {
            @Override
            public void run()
            {
                for (CommonUserVO commonUserVO : list) {
                    String message = commonUserVO.getUserName()+"，您好！在线监督平台提示:关于"+exposureArrange.getArrangeTitle()+"的曝光已发出，请注意查看";
                    String mobileNo = commonUserVO.getPhonenumber();
                    if(StringUtils.isNotEmpty(mobileNo)){
                        SmsUtil.sendMessage(message, mobileNo);
                    }
                }
            }
        };
    }

    private TimerTask createWebsiteTips(List<CommonUserVO> list, int num) {
        return new TimerTask()
        {
            @Override
            public void run()
            {
                for (CommonUserVO commonUserVO : list) {
                    WorkHandle workHandle = new WorkHandle();
                    workHandle.setUserId(commonUserVO.getUserId());
                    workHandle.setType(CommonConstants.WORK);
                    workHandleService.insertOrUpdateWorkHandle(workHandle, num); //工作项+1
                }
            }
        };
    }
}
